/*
 * ModifyPerson.java
 */

package nxtgeninventorysystem.View.Dialog;

import java.awt.event.ActionEvent;
import java.util.HashMap;
import javax.swing.AbstractAction;
import javax.swing.DefaultComboBoxModel;
import nxtgeninventorysystem.Controller.Actions;
import nxtgeninventorysystem.Data.Person;
import nxtgeninventorysystem.View.Gui;
import nxtgeninventorysystem.View.Gui.ComboBoxType;

/**
 * The dialog box that displays fields for person creation.
 * @author Seraph Softworks
 */
public class ModifyPerson extends GuiDialog {

    /** Creates new form DatabaseConfig */
    public ModifyPerson(java.awt.Frame parent) {
        super(parent);
    }

    @Override
    public void resetFields() {
        if(target == null) {
            personContactNoValue.setText("");
            personNameValue.setText("");
            personTeamValue.setSelectedIndex(0);
        }
        else {
            personContactNoValue.setText(target.getContactNo());
            personNameValue.setText(target.getName());
            personTeamValue.setSelectedItem(target.getTeam());
        }
    }

    @Override
    public void refreshComboBoxes(HashMap<ComboBoxType, DefaultComboBoxModel> models) {
        personTeamValue.setModel(models.get(Gui.ComboBoxType.TeamsComboBox));
    }

    @Override
    protected void initialize() {
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        personNameValue = new javax.swing.JTextField();
        personTeamValue = new javax.swing.JComboBox();
        personContactNoValue = new javax.swing.JTextField();
        javax.swing.JLabel personContactNoLabel = new javax.swing.JLabel();
        javax.swing.JLabel personTeamLabel = new javax.swing.JLabel();
        javax.swing.JLabel personNameLabel = new javax.swing.JLabel();
        okButton = new javax.swing.JButton();
        closeButton = new javax.swing.JButton();
        createTeamButton = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(nxtgeninventorysystem.Driver.class).getContext().getResourceMap(ModifyPerson.class);
        setTitle(resourceMap.getString("Form.title")); // NOI18N
        setMinimumSize(new java.awt.Dimension(295, 126));
        setName("Form"); // NOI18N
        setResizable(false);

        personNameValue.setName("personNameValue"); // NOI18N

        personTeamValue.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "--Choose Type--" }));
        personTeamValue.setName("personTeamValue"); // NOI18N

        personContactNoValue.setName("personContactNoValue"); // NOI18N

        personContactNoLabel.setText(resourceMap.getString("personContactNoLabel.text")); // NOI18N
        personContactNoLabel.setName("personContactNoLabel"); // NOI18N

        personTeamLabel.setText(resourceMap.getString("personTeamLabel.text")); // NOI18N
        personTeamLabel.setName("personTeamLabel"); // NOI18N

        personNameLabel.setText(resourceMap.getString("personNameLabel.text")); // NOI18N
        personNameLabel.setName("personNameLabel"); // NOI18N

        javax.swing.ActionMap actionMap = org.jdesktop.application.Application.getInstance(nxtgeninventorysystem.Driver.class).getContext().getActionMap(ModifyPerson.class, this);
        okButton.setAction(actionMap.get("ok")); // NOI18N
        okButton.setText(resourceMap.getString("okButton.text")); // NOI18N
        okButton.setName("okButton"); // NOI18N

        closeButton.setAction(new AbstractAction() {
            public void actionPerformed(ActionEvent e) {
                dispose();
                Gui.setStatusBarMessage("Ready...");
            }
        });
        closeButton.setText(resourceMap.getString("closeButton.text")); // NOI18N
        closeButton.setName("closeButton"); // NOI18N

        createTeamButton.setAction(new AbstractAction() {
            public void actionPerformed(ActionEvent e) {
                Actions.person.gotoCreateTeam();
            }
        });
        createTeamButton.setText(resourceMap.getString("createTeamButton.text")); // NOI18N
        createTeamButton.setName("createTeamButton"); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(personNameLabel)
                            .addComponent(personTeamLabel)
                            .addComponent(personContactNoLabel))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(personNameValue, javax.swing.GroupLayout.DEFAULT_SIZE, 213, Short.MAX_VALUE)
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                .addComponent(personTeamValue, 0, 112, Short.MAX_VALUE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(createTeamButton))
                            .addComponent(personContactNoValue, javax.swing.GroupLayout.DEFAULT_SIZE, 213, Short.MAX_VALUE)))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(okButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(closeButton)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(personNameValue, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(personNameLabel))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(personTeamValue, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(personTeamLabel)
                    .addComponent(createTeamButton))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(personContactNoValue, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(personContactNoLabel))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(closeButton)
                    .addComponent(okButton))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton closeButton;
    private javax.swing.JButton createTeamButton;
    private javax.swing.JButton okButton;
    private javax.swing.JTextField personContactNoValue;
    private javax.swing.JTextField personNameValue;
    private javax.swing.JComboBox personTeamValue;
    // End of variables declaration//GEN-END:variables

    private Person target = null;

    public void setTargetPerson(Person person) {
        target = person;
    }

    public void dropTargetPerson() {
        target = null;
    }

    public Person getFormPerson() {
        Person form = new Person();
        form.setName(personNameValue.getText());
        form.setContactNo(personContactNoValue.getText());
        form.setTeam((String) personTeamValue.getSelectedItem());
        return form;
    }

    public Person getTargetPerson() {
        return target;
    }

    @Override
    public void setOkAction(javax.swing.Action action) {
        okButton.setAction(action);
        okButton.setText("Ok");
    }

}
